package com.guigu.yh.springcloud;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

/**
 * 带熔断的支付服务
 *
 * @author 张颖辉（zyh）
 * @version 1.0.0
 * @date 2022/08/22
 */

//激活服务降级、熔断、限流组件 hystrix ，注意：@EnableHystrix涵盖了@EnableCircuitBreaker
@EnableCircuitBreaker
//当前服务是client, 需要注册到EurekaServer
@EnableEurekaClient
@SpringBootApplication
public class PaymentHystrixMain8008 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8008.class, args);
    }


    /**
     * 此配置是为了服务监控而存在，与服务容错本身无关，springcloud升级以后的坑
     * 避免本服务监控接口地址404，监控接口地址：http://localhost:8008/payment/hystrix.stream
     * 只要在自己的项目中配置上下面的servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
